<?php
	class models_mathang extends connect{
		function listMatHang(){
			$sql = "SELECT MaMH, MaLH, TenMH, DVT, SoLuong, Gia, HinhAnh, MoTa, AnHien
					FROM mathang";
			$this->query_db($sql);
			return $this->fetchAll();
		}

		function tongsodong()
		{
			$sql="SELECT * from mathang";
			$this->query_db($sql);
			return $this->totalRow();
	
		}
	
		function danhsachmh($start,$limit){
			$sql = "SELECT MaMH, MaLH, TenMH, DVT, SoLuong, Gia, HinhAnh, MoTa, AnHien
					FROM mathang 
					WHERE AnHien=1
					LIMIT $start,$limit";
			$this->query_db($sql);
			return $this->fetchAll();
		}
				
		function addMatHang($MaLH, $TenMH, $DVT, $Gia, $HinhAnh, $MoTa, $AnHien){
			$sql = "INSERT INTO mathang VALUES ('','$MaLH','$TenMH','$DVT','','$Gia', '$HinhAnh','$MoTa','$AnHien')";
			$this->query_db($sql);
			if(!$this->result){
				die (mysql_error($this->conn)); //Bat loi tu mysql trigger
				echo '<script> window.location.href="index.php?mod=addmathang"</script>';
			}
		}
		
		function delMatHang($intMa){
			$sql = "DELETE
					FROM mathang
					WHERE MaMH = '$intMa'";
			$this->query_db($sql);	
		}
		
		function getMatHang($intMa)
		{
			$sql = "SELECT MaMH, MaLH, TenMH, DVT, SoLuong,Gia, HinhAnh, MoTa, AnHien
					FROM mathang 
					WHERE MaMH = '$intMa'";
			
			$this->query_db($sql);
			return $this->fetchOne();
		}
	
		
		function updateMatHang($MaLH, $TenMH, $HinhAnh, $Gia, $DVT, $MoTa, $AnHien, $id)
		{
			$sql ="	UPDATE mathang
					SET 
						MaLH = '$MaLH', 
						TenMH = '$TenMH',
						HinhAnh = '$HinhAnh', 
						Gia = '$Gia', 
						DVT = '$DVT',
						MoTa = '$MoTa', 
						AnHien = '$AnHien'
					WHERE MaMH = '$id'";
			$this->query_db($sql);
		}
			function updatemh($SoLuong, $id)
		{
			$sql ="	UPDATE mathang
					SET 
						SoLuong = '$SoLuong'
					WHERE MaMH = '$id'";
			$this->query_db($sql);
		}
		function tongsodongid($id)
		{
			$sql="SELECT * from mathang
				WHERE MaLH=$id";
			$this->query_db($sql);
			return $this->totalRow();
	
		}
		function _list($start,$limit,$id)
		{
			$sql = "SELECT  MaMH, MaLH, TenMH, DVT, SoLuong,Gia, HinhAnh, MoTa, AnHien
					FROM mathang
					WHERE MaLH = '$id' and AnHien = 1
					LIMIT $start,$limit";
			$this->query_db($sql);
			return $this->fetchAll();
			
		}
		function _listAll()
		{
			$sql = "SELECT MaMH, MaLH, TenMH, DVT, SoLuong,Gia, HinhAnh, MoTa, AnHien
					FROM mathang
					WHERE AnHien = 1";
			$this->query_db($sql);
			return $this->fetchAll();
			
		}
		function _detail($id)
		{
			$sql = "SELECT  MaMH, MaLH, TenMH, DVT, SoLuong,Gia, HinhAnh, MoTa, AnHien
					FROM mathang
					WHERE MaMH = '$id' and AnHien = 1";
			$this->query_db($sql);
			return $this->fetchOne();
			
		}
		function _find($value)
		{
			$sql = "SELECT MaMH, MaLH, TenMH, DVT, SoLuong,Gia, HinhAnh, MoTa, AnHien
			FROM mathang 
			WHERE TenMH LIKE '%$value%' AND AnHien=1";
			$this->query_db($sql);
			return $this->fetchAll();
		}
		
	
		
	}
	
	
class pagers{ 
    function findstart($limit) 
    { 
        if ((!isset($_GET['page'])) || ($_GET['page']=="1")) 
        { 
            $start=0; 
            $_GET['page']=1; 
        } 
        else 
        { 
            $start=($_GET['page']-1)*$limit; 
        } 
        return $start; 
    } 
    function findpages($count,$limit) 
    { 
        $pages=(($count % $limit)==0) ? $count / $limit : floor($count / $limit)+1; 
        return $pages; 
    } 
    function pagelist($curpage , $pages) 
    { 
        $page_list=""; 
       
        if(($curpage !=1) && ($curpage)) 
        { 
            $page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=mathang"."&page=1\" title=\"Trang đầu\"> << </a> "; 
        } 
        if(($curpage-1) > 0) 
        { 
           	$page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=mathang"."&page=".($curpage-1)."\" title=\"Trang trước\"> < </a>"; 	
        } 
        for ($i=1; $i<=$pages; $i++) 
        { 
            if($i==$curpage) 
            { 
                $page_list .="<b>".$i."</b>"; 
            } 
            else 
            { 
                $page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=mathang"."&page=".$i."\" title=\"Trang".$i."\">".$i."</a>"; 
            } 
            $page_list .=" "; 
        } 
        if (($curpage+1) <=$pages) 
        { 
			$page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=mathang"."&page=".($curpage+1)."\" title=\"Trang sau\"> > </a>"; 	
		}
		if(($curpage !=$pages) && ($pages != 0)){
		
            $page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=mathang"."&page=".$pages."\" title=\"Trang cuối\"> >> </a>"; 
        } 
		
        $page_list .="</td>\n\n"; 
        return $page_list; 
    } 
    function nextprev($curpage,$pages) 
    { 
        $next_prev=""; 
        if(($curpage-1)<=0) 
        { 
            $next_prev .="Ve trang truoc";            
        } 
            else 
            { 
                $next_prev .="<a href=\"".$_SERVER['PHP_SELF']."?mod=mathang"."&page=".($curpage-1)."\"> Trang truoc</a>"; 
            } 
            $next_prev .=" || "; 
            if (($curpage+1) > $pages) 
            { 
                $next_prev .="Den trang sau"; 
            } 
            else 
            { 
                $next_prev .="<a href=\"".$_SERVER['PHP_SELF']."?mod=mathang"."&page=".($curpage+1)."\"> Trang sau</a>"; 
            } 
            return $next_prev; 
    } 
}

class pagermain 
{
    function findstart($limit) 
    { 
        if ((!isset($_GET['page'])) || ($_GET['page']=="1")) 
        { 
            $start=0; 
            $_GET['page']=1; 
        } 
        else 
        { 
            $start=($_GET['page']-1)*$limit; 
        } 
        return $start; 
    } 
    function findpages($count,$limit) 
    { 
        $pages=(($count % $limit)==0) ? $count / $limit : floor($count / $limit)+1; 
        return $pages; 
    } 
    function pagelist($curpage , $pages) 
    { 
        $page_list=""; 
        /*in trang dau tien va nhung link toi trang neu co */ 
        if(($curpage !=1) && ($curpage)) 
        { 
            $page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=sanpham&id=".$_GET['id']."&page=1\" title=\"Trang đầu\"> << </a> "; 
        } 
        if(($curpage-1) > 0) 
        { 
           	$page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=sanpham&id=".$_GET['id']."&page=".($curpage-1)."\" title=\"Trang trước\"> < </a>"; 	
        } 
        for ($i=1; $i<=$pages; $i++) 
        { 
            if($i==$curpage) 
            { 
                $page_list .="<b>".$i."</b>"; 
            } 
            else 
            { 
                $page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=sanpham&id=".$_GET['id']."&page=".$i."\" title=\"Trang".$i."\">".$i."</a>"; 
            } 
            $page_list .=" "; 
        } 
        if (($curpage+1) <=$pages) 
        { 
			$page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=sanpham&id=".$_GET['id']."&page=".($curpage+1)."\" title=\"Trang sau\"> > </a>"; 	
		}
		if(($curpage !=$pages) && ($pages != 0)){
		
            $page_list .="<a href=\"".$_SERVER['PHP_SELF']."?mod=sanpham&id=".$_GET['id']."&page=".$pages."\" title=\"Trang cuối\"> >> </a>"; 
        } 
		
        $page_list .="</td>\n\n"; 
        return $page_list; 
    } 
    function nextprev($curpage,$pages) 
    { 
        $next_prev=""; 
        if(($curpage-1)<=0) 
        { 
            $next_prev .="Ve trang truoc";            
        } 
            else 
            { 
                $next_prev .="<a href=\"".$_SERVER['PHP_SELF']."?mod=mathang"."&page=".($curpage-1)."\"> Trang truoc</a>"; 
            } 
            $next_prev .=" || "; 
            if (($curpage+1) > $pages) 
            { 
                $next_prev .="Den trang sau"; 
            } 
            else 
            { 
                $next_prev .="<a href=\"".$_SERVER['PHP_SELF']."?mod=mathang"."&page=".($curpage+1)."\"> Trang sau</a>"; 
            } 
            return $next_prev; 
    } 
}


?>